package jbase.project.system.net.http;

import jbase.project.system.util.RegUtil;

public class HttpResponseHeaders extends HttpHeaders {

	public HttpResponseHeaders() {
		super();
	}

	public HttpResponseHeaders(String header) {
		super(header);
	}

	public void setConentLength(int contentLength) {
		super.insertOrUpdateLine("\\s*" + RegUtil.toRegxIC("Content-Length") + "\\s*:.*",
				"Content-Length:" + contentLength);
	}

	public void setTransferEncoding(String transferEncoding) {
		super.insertOrUpdateLine("\\s*" + RegUtil.toRegxIC("Transfer-Encoding") + "\\s*:.*",
				"Transfer-Encoding:" + transferEncoding);
	}

	public void setContentType(String contentType) {
		super.insertOrUpdateLine("\\s*" + RegUtil.toRegxIC("Content-Type") + "\\s*:.*", "Content-Type:" + contentType);
	}

	public void setResponseStatus(int status) {
		int size = super.lineCount();
		String statusLine = "HTTP/1.1 503 BadGetway";
		switch (status) {
		case 200:
			statusLine = "HTTP/1.1 200 OK";
			break;
		case 404:
			statusLine = "HTTP/1.1 404 NotFound";
			break;
		}
		if (size == 0) {
			super.insertLine(statusLine);
		} else {
			if (super.lineMatch(0, "HTTP/1\\.\\d\\s+\\d+\\s.*")) {
				super.setLine(0, statusLine);
			} else {
				super.insertLine(0, statusLine);
			}
		}
	}

	@Override
	public String toString() {
		return super.toString();
	}
}
